package com.alameda.service.manage;

import com.alameda.api.manage.BusinessIndicatorService;
import com.alameda.api.manage.ManageService;
import com.alameda.api.manage.TechnicalIndicatorService;
import com.alameda.api.statement.SystemRecordService;
import com.alameda.manage.bo.ManageBO;
import com.alameda.manage.dto.ManageDTO;
import com.alameda.manage.enums.ManageEnum;
import com.alameda.manage.pojo.ManageDO;
import com.alameda.manage.vo.ManageVO;
import com.alameda.mapper.manage.ManageMapper;
import com.alameda.statement.enums.SystemRecordEnum;
import com.alameda.utils.ExcelUtils;
import com.alameda.utils.ObjectUtils;
import com.alameda.utils.PubUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目管理 业务逻辑层 具体实现
 *
 * @author Lv
 * @date 2024/08/04
 */
@Slf4j
@Service
public class ManageServiceImpl extends ServiceImpl<ManageMapper, ManageDO> implements ManageService {

    @Autowired
    private ManageMapper manageMapper;

    /**
     * 查询项目管理列表
     *
     * @param manageDTO 项目管理数据传递对象
     * @return
     */
    @Override
    public IPage<ManageVO> loadManage(ManageDTO manageDTO) {
        IPage<ManageDO> iPage = new Page<>(manageDTO.getPageNum(), manageDTO.getPageSize());

        LambdaQueryWrapper<ManageDO> lambdaQueryWrapper = Wrappers.lambdaQuery(ManageDO.class)
                .like(PubUtils.isNotEmpty(manageDTO.getProjectName()), ManageDO::getProjectName, manageDTO.getProjectName())
                .eq(PubUtils.isNotNull(manageDTO.getProjectState()), ManageDO::getProjectState, manageDTO.getProjectState())
                .orderByDesc(ManageDO::getCreateTime);

        IPage<ManageDO> page = this.page(iPage, lambdaQueryWrapper);

        List<ManageVO> manageVOList = page.getRecords().stream().map(item -> {
            ManageVO manageVO = ObjectUtils.convert(item, ManageVO.class);
            manageVO.setProjectStateRemark(ManageEnum.getValueByKey(item.getProjectState()));
            return manageVO;
        }).collect(Collectors.toList());

        IPage<ManageVO> pageData = ObjectUtils.convertPageData(page, ManageVO.class);
        pageData.setRecords(manageVOList);
        return pageData;
    }

    @Autowired
    private SystemRecordService systemRecordService;

    /**
     * 添加或修改项目管理信息
     *
     * @param manageDTO 项目管理数据传递对象
     * @return
     */
    @Override
    public Boolean addOrEditManage(ManageDTO manageDTO) {
        ManageDO manageDO = ObjectUtils.convert(manageDTO, ManageDO.class);
        systemRecordService.saveRecord(SystemRecordEnum.MESSAGE_COUNT);
        return this.saveOrUpdate(manageDO);
    }

    /**
     * 导出excel
     *
     * @param manageDTO 项目管理数据传递对象
     */
    @Override
    public void export(ManageDTO manageDTO) {
        List<ManageDO> manageDOList = getExportData(manageDTO);

        List<ManageBO> manageBOList = manageDOList.stream().map(item -> {
            ManageBO manageBO = ObjectUtils.convert(item, ManageBO.class);
            manageBO.setProjectState(ManageEnum.getValueByKey(item.getProjectState()));
            manageBO.setProjectStartTime(formatData(item.getProjectStartTime()));
            manageBO.setProjectEndTime(formatData(item.getProjectEndTime()));
            manageBO.setProjectSchedule(PubUtils.isEmpty(item.getProjectSchedule()) ? "" : item.getProjectSchedule() + "%");
            return manageBO;
        }).collect(Collectors.toList());

        ExcelUtils.export(manageBOList, ManageBO.class, "项目管理信息");
    }

    @Autowired
    private BusinessIndicatorService businessIndicatorService;

    @Autowired
    private TechnicalIndicatorService technicalIndicatorService;

    /**
     * 删除项目相关信息
     *
     * @param idList 项目id集合
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean removeByIdList(List<Long> idList) {
        technicalIndicatorService.removeByProjectIdList(idList);
        businessIndicatorService.removeByProjectIdList(idList);
        return this.removeBatchByIds(idList);
    }

    /**
     * 格式化日期 年-月-日
     *
     * @param date 日期
     * @return
     */
    private String formatData(Date date) {
        if (PubUtils.isNull(date)) {
            return "";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(date);
    }

    /**
     * 获取导出数据
     *
     * @param manageDTO 项目管理数据传递对象
     * @return
     */
    private List<ManageDO> getExportData(ManageDTO manageDTO) {
        if (PubUtils.isEmpty(manageDTO.getIdList())) {
            IPage<ManageVO> pageData = loadManage(manageDTO);
            List<ManageVO> records = pageData.getRecords();
            return ObjectUtils.convertList(records, ManageDO.class);
        } else {
            return this.findByIdInOrderByCreateTimeDesc(manageDTO.getIdList());
        }
    }
}
